/**
 * calculator
 *
 * @author : tzy
 * @Date : 2021-03-22
 * @Description:
 **/

package com.tang.calculator.functions;

import com.tang.calculator.context.CalculateContext;
import com.tang.calculator.symbols.ValueSymbol;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Stack;

@Component
public class CaseFunction extends AbstractFunction {
    @Override
    public String getFunctionName() {
        return "case";
    }

    @Override
    public void run(CalculateContext context, ValueSymbol... args) {
        if (args.length != 3) {
            throw new RuntimeException("case函数必须要有三个参数");
        }
        Stack<ValueSymbol> valueStack = context.getValueStack();
        ValueSymbol condition = args[0];
        if (condition.getValue().compareTo(BigDecimal.ZERO) == 0) {
            valueStack.push(args[2]);
        } else {
            valueStack.push(args[1]);
        }

    }
}
